package com.vancouver.knows.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BSharedPrefManager;

public class ChooseLanguageActivity extends BaseActivity {
	private Context mContext;
	@Bind(R.id.common_tips)
	TextView mCommonTips;
	@Bind(R.id.common_back)
	ImageView mCommonBack;

	@Bind(R.id.language_zh)
	TextView languageZh;
	@Bind(R.id.language_tw)
	TextView languageTw;
	@Bind(R.id.language_eh)
	TextView languageEh;
	@Bind(R.id.image_zh)
	ImageView imageZh;
	@Bind(R.id.image_tw)
	ImageView imageTw;
	@Bind(R.id.image_eh)
	ImageView imageEh;

	private final String LANGUAGE_ZH = "zh";
	private final String LANGUAGE_TW = "tw";
	private final String LANGUAGE_EH = "eh";
	private BSharedPrefManager sharedPrefManager;
	private String chooseWhitch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_language);
		mContext = ChooseLanguageActivity.this;
		setViews();
	}

	private void setViews() {
		Intent intent = getIntent();
		String tips = intent.getStringExtra("COMMON_TITLE");
		mCommonTips.setText(tips);
		sharedPrefManager = imContext.getSharedPrefManager();
		setCurrentLanguageChoose();
	}

	private void setCurrentLanguageChoose() {
		imageZh.setVisibility(View.GONE);
		imageTw.setVisibility(View.GONE);
		imageEh.setVisibility(View.GONE);
		chooseWhitch = sharedPrefManager.getChooseLanguage();
		if (chooseWhitch == null || chooseWhitch.equals(LANGUAGE_ZH)) {
			imageZh.setVisibility(View.VISIBLE);
		} else if (chooseWhitch.equals(LANGUAGE_TW)) {
			imageTw.setVisibility(View.VISIBLE);
		} else if (chooseWhitch.equals(LANGUAGE_EH)) {
			imageEh.setVisibility(View.VISIBLE);
		}
	}

	private void sendBroadcast(String whichLanguage) {
		Intent intent = new Intent();
		intent.putExtra("CHOOSE_LANGUAGE", whichLanguage);
		intent.setAction(PhoConstants.CHOOSE_LANGUAGE);
		mContext.sendBroadcast(intent);

	}

	@OnClick(R.id.common_back)
	public void onCommonBackClick() {
		finish();
	}

	@OnClick(R.id.layout_zh)
	public void onChooseZhClick() {
		sendBroadcast(LANGUAGE_ZH);
		if (sharedPrefManager != null) {
			sharedPrefManager.saveChooseLanguage(LANGUAGE_ZH);
		}
		onCommonBackClick();
	}

	@OnClick(R.id.layout_tw)
	public void onChooseTwClick() {
		sendBroadcast(LANGUAGE_TW);
		if (sharedPrefManager != null) {
			sharedPrefManager.saveChooseLanguage(LANGUAGE_TW);
		}
		onCommonBackClick();
	}

	@OnClick(R.id.layout_eh)
	public void onChooseEhClick() {
		sendBroadcast(LANGUAGE_EH);
		if (sharedPrefManager != null) {
			sharedPrefManager.saveChooseLanguage(LANGUAGE_EH);
		}
		onCommonBackClick();
	}

}
